package com.stone.framework.cases.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.framework.cases.mapper.CaseInfoMapper;
import com.stone.framework.cases.model.CaseConditionDTO;
import com.stone.framework.cases.model.CaseDTO;
import com.stone.framework.cases.service.CaseService;
import com.stone.framework.core.util.Beans;
import com.stone.framework.core.util.Pages;
import com.stone.framework.message.cases.AllCaseReqBody;
import com.stone.framework.message.cases.AllCaseResBody;
import com.stone.framework.message.cases.CurrCaseReqBody;
import com.stone.framework.message.cases.CurrCaseResBody;

@Service
@Transactional(readOnly = true)
public class CaseServiceImpl implements CaseService {

    @Autowired
    private CaseInfoMapper caseInfoMapper;

    @Override
    public IPage<CurrCaseResBody> queryCurrCases(CurrCaseReqBody reqBody, Pageable pageable) {
        CaseConditionDTO condition = new CaseConditionDTO();
        Beans.copyProperties(reqBody, condition);
        Page<CaseDTO> page = Pages.<CaseDTO>convert(pageable, reqBody.getOrderBy()); // shit
        return caseInfoMapper.selectCurrCases(page, caseInfoMapper.condition(condition)).convert(dto -> {
            CurrCaseResBody resBody = new CurrCaseResBody();
            Beans.copyProperties(dto, resBody);
            return resBody;
        });
    }

    @Override
    public IPage<AllCaseResBody> queryAllCases(AllCaseReqBody reqBody, Pageable pageable) {
        CaseConditionDTO condition = new CaseConditionDTO();
        Beans.copyProperties(reqBody, condition);
        Page<CaseDTO> page = Pages.<CaseDTO>convert(pageable, reqBody.getOrderBy()); // shit
        return caseInfoMapper.selectAllCases(page, caseInfoMapper.condition(condition)).convert(dto -> {
            AllCaseResBody resBody = new AllCaseResBody();
            Beans.copyProperties(dto, resBody);
            return resBody;
        });
    }

}
